import pyecharts.options as opts
from pyecharts.globals import CurrentConfig
from sqlalchemy import text
from pyecharts.charts import Line, Tab
from exts import db

CurrentConfig.ONLINE_HOST = "/static/"


def did1(a):
    a = str(a)
    class_ids1 = []
    big_class_ids = []
    big_psumdatas = []
    big_vsumdatas = []
    hids = []
    psumdatas1 = []
    vsumdatas1 = []
    sql1 = "select DISTINCT class_id1 from copyversion  where class_id2="
    sql2 = 'select DISTINCT hid from copyversion ORDER BY hid ASC'
    sql3 = 'select vsum,csum from copyversion where class_id1="'
    sql4 = ' ORDER BY hid'
    sql5 = "select vsum,csum from copyversion where class_id1='统计大班' and class_id2 ="
    sql6 = "select DISTINCT class_id2 from copyversion"
    sql7 = " ORDER by hid"
    datas = db.session.execute(text(sql1 + a)).all()
    for data in datas:
        class_ids1.append(data[0])
        psumdatas1.append([])
        vsumdatas1.append([])
    datas = db.session.execute(text(sql2)).all()
    for data in datas:
        hids.append(data[0])
    i1 = 0
    for class_id in class_ids1:
        if class_id == "统计大班":
            i1 += 1
            continue
        datas = db.session.execute(text(sql3 + str(class_id) + '" and class_id2=' + a + sql4)).all()
        for data in datas:
            vsumdatas1[i1].append(data[0])
            psumdatas1[i1].append(data[1])
        i1 += 1
    datas = db.session.execute(text(sql6)).all()
    for data in datas:
        big_class_ids.append(data[0])
        big_psumdatas.append([])
        big_vsumdatas.append([])
    i = 0
    for big_class_id in big_class_ids:
        datas = db.session.execute(text(sql5 + str(big_class_id) + sql7)).all()
        for data in datas:
            big_vsumdatas[i].append(data[0])
            big_psumdatas[i].append(data[1])
        i += 1
    db.session.commit()
    l = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业抄袭人数"))
    l1 = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业版本数"))
    l2 = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业抄袭总人数"))
    l3 = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业总版本数"))
    l.add_xaxis(xaxis_data=[str(value) for value in hids])
    l1.add_xaxis(xaxis_data=[str(value) for value in hids])
    l2.add_xaxis(xaxis_data=[str(value) for value in hids])
    l3.add_xaxis(xaxis_data=[str(value) for value in hids])
    j = 0
    while j < i1:
        if class_ids1[j] == "统计大班":
            j += 1
            continue
        l.add_yaxis(series_name=class_ids1[j],
                    y_axis=vsumdatas1[j],
                    linestyle_opts=opts.LineStyleOpts(width=1.5)
                    )
        l1.add_yaxis(series_name=class_ids1[j],
                     y_axis=psumdatas1[j],
                     linestyle_opts=opts.LineStyleOpts(width=1.5)
                     )
        j += 1
    j = 0
    while j < i:
        l2.add_yaxis(series_name="班级" + big_class_ids[j],
                     y_axis=big_vsumdatas[j],
                     linestyle_opts=opts.LineStyleOpts(width=1.5)
                     )
        l3.add_yaxis(series_name="班级" + big_class_ids[j],
                     y_axis=big_psumdatas[j],
                     linestyle_opts=opts.LineStyleOpts(width=1.5)
                     )
        j += 1
    l.set_global_opts(
        title_opts=opts.TitleOpts(title=a + "班历次作业版本数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="版本数量", name_location='middle')
    )
    l1.set_global_opts(
        title_opts=opts.TitleOpts(title=a + "班历次作业抄袭总人数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="抄袭人数", name_location='middle')
    )
    l2.set_global_opts(
        title_opts=opts.TitleOpts(title="大班历次作业版本数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="版本数量", name_location='middle')
    )
    l3.set_global_opts(
        title_opts=opts.TitleOpts(title="大班历次作业抄袭总人数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="抄袭人数", name_location='middle')
    )
    tab = Tab(page_title="图表")
    tab.add(l2, "大班历次作业版本数")
    tab.add(l3, "大班历次作业抄袭人数")
    tab.add(l, tab_name=a + "班历次作业版本数")
    tab.add(l1, tab_name=a + "班历次作业抄袭人数")
    tab.render("./templates/line_base.html")


def did2(a, b):
    a = str(a)
    b = str(b)
    class_ids1 = []
    class_ids3 = []
    big_class_ids = []
    big_psumdatas = []
    big_vsumdatas = []
    hids = []
    psumdatas1 = []
    vsumdatas1 = []
    psumdatas3 = []
    vsumdatas3 = []
    sql1 = "select DISTINCT class_id1 from copyversion  where class_id2="
    sql2 = 'select DISTINCT hid from copyversion ORDER BY hid ASC'
    sql3 = 'select vsum,csum from copyversion where class_id1="'
    sql4 = ' ORDER BY hid'
    sql5 = "select vsum,csum from copyversion where class_id1='统计大班' and class_id2 ="
    sql6 = "select DISTINCT class_id2 from copyversion"
    sql7 = " ORDER by hid"
    datas = db.session.execute(text(sql1 + a)).all()
    for data in datas:
        class_ids1.append(data[0])
        psumdatas1.append([])
        vsumdatas1.append([])
    datas = db.session.execute(text(sql1 + b)).all()
    for data in datas:
        class_ids3.append(data[0])
        psumdatas3.append([])
        vsumdatas3.append([])
    datas = db.session.execute(text(sql2)).all()
    for data in datas:
        hids.append(data[0])
    i1 = 0
    for class_id in class_ids1:
        if class_id == "统计大班":
            i1 += 1
            continue
        datas = db.session.execute(text(sql3 + str(class_id) + '" and class_id2=' + a + sql4)).all()
        for data in datas:
            vsumdatas1[i1].append(data[0])
            psumdatas1[i1].append(data[1])
        i1 += 1
    i2 = 0
    for class_id in class_ids3:
        if class_id == "统计大班":
            i2 += 1
            continue
        datas = db.session.execute(text(sql3 + str(class_id) + '" and class_id2=' + b + sql4)).all()
        for data in datas:
            vsumdatas3[i2].append(data[0])
            psumdatas3[i2].append(data[1])
        i2 += 1
    datas = db.session.execute(text(sql6)).all()
    for data in datas:
        big_class_ids.append(data[0])
        big_psumdatas.append([])
        big_vsumdatas.append([])
    i = 0
    for big_class_id in big_class_ids:
        datas = db.session.execute(text(sql5 + str(big_class_id) + sql7)).all()
        for data in datas:
            big_vsumdatas[i].append(data[0])
            big_psumdatas[i].append(data[1])
        i += 1
    db.session.commit()
    # print(class_ids1,class_ids3,big_class_ids,psumdatas1,vsumdatas1,psumdatas3,vsumdatas3)
    l = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业抄袭人数"))
    l1 = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业版本数"))
    l2 = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业抄袭总人数"))
    l3 = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业总版本数"))
    l4 = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业抄袭人数"))
    l5 = Line(init_opts=opts.InitOpts(width="45%", page_title="历次作业版本数"))
    l.add_xaxis(xaxis_data=[str(value) for value in hids])
    l1.add_xaxis(xaxis_data=[str(value) for value in hids])
    l2.add_xaxis(xaxis_data=[str(value) for value in hids])
    l3.add_xaxis(xaxis_data=[str(value) for value in hids])
    l4.add_xaxis(xaxis_data=[str(value) for value in hids])
    l5.add_xaxis(xaxis_data=[str(value) for value in hids])
    j = 0
    while j < i1:
        if class_ids1[j] == "统计大班":
            j += 1
            continue
        l.add_yaxis(series_name=class_ids1[j],
                    y_axis=vsumdatas1[j],
                    linestyle_opts=opts.LineStyleOpts(width=1.5)
                    )
        l1.add_yaxis(series_name=class_ids1[j],
                     y_axis=psumdatas1[j],
                     linestyle_opts=opts.LineStyleOpts(width=1.5)
                     )
        j += 1
    j = 0
    while j < i2:
        if class_ids3[j] == "统计大班":
            j += 1
            continue
        l4.add_yaxis(series_name=class_ids3[j],
                     y_axis=vsumdatas3[j],
                     linestyle_opts=opts.LineStyleOpts(width=1.5)
                     )
        l5.add_yaxis(series_name=class_ids3[j],
                     y_axis=psumdatas3[j],
                     linestyle_opts=opts.LineStyleOpts(width=1.5)
                     )
        j += 1
    j = 0
    while j < i:
        l2.add_yaxis(series_name="班级" + big_class_ids[j],
                     y_axis=big_vsumdatas[j],
                     linestyle_opts=opts.LineStyleOpts(width=1.5)
                     )
        l3.add_yaxis(series_name="班级" + big_class_ids[j],
                     y_axis=big_psumdatas[j],
                     linestyle_opts=opts.LineStyleOpts(width=1.5)
                     )
        j += 1
    l.set_global_opts(
        title_opts=opts.TitleOpts(title=a + "班历次作业版本数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="版本数量", name_location='middle')
    )
    l1.set_global_opts(
        title_opts=opts.TitleOpts(title=a + "班历次作业抄袭总人数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="抄袭人数", name_location='middle')
    )
    l4.set_global_opts(
        title_opts=opts.TitleOpts(title=b + "班历次作业版本数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="版本数量", name_location='middle')
    )
    l5.set_global_opts(
        title_opts=opts.TitleOpts(title=b + "班历次作业抄袭总人数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="抄袭人数", name_location='middle')
    )
    l2.set_global_opts(
        title_opts=opts.TitleOpts(title="大班历次作业版本数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="版本数量", name_location='middle')
    )
    l3.set_global_opts(
        title_opts=opts.TitleOpts(title="大班历次作业抄袭总人数"),
        legend_opts=opts.LegendOpts(pos_top="25px", pos_bottom="25px"),
        tooltip_opts=opts.TooltipOpts(trigger="axis"),
        toolbox_opts=opts.ToolboxOpts(is_show=True),
        xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False, name="作业编号"),
        yaxis_opts=opts.AxisOpts(name="抄袭人数", name_location='middle')
    )
    tab = Tab(page_title="图表")
    tab.add(l2, "大班历次作业版本数")
    tab.add(l3, "大班历次作业抄袭人数")
    tab.add(l, tab_name=a + "班历次作业版本数")
    tab.add(l1, tab_name=a + "班历次作业抄袭人数")
    tab.add(l4, tab_name=b + "班历次作业版本数")
    tab.add(l5, tab_name=b + "班历次作业抄袭人数")
    # page.add(l2, l3, l, l1)
    tab.render("./templates/line_base.html")
